package com.briup.homework.chap10;

/**
 * 模拟归途赛跑的流程
 */
public class Test11_Race {
    public static void main(String[] args) {
        Thread t1 = new Rabbit("兔子");
        Thread t2 = new Turtle("乌龟");
        t1.start();
        t2.start();
    }
}
// 兔子线程
class Rabbit extends Thread {
    private int distance;
    public Rabbit(String name) {
        super(name);
    }
    @Override
    public void run() {
        String name = Thread.currentThread().getName();
        // 取一个随机数
        while (distance<100) {
            double d = Math.random();
            if (d < 0.3) {
                distance += 2;
                System.out.println("---"+name+"跑了"+distance+"米");
            }
        }
        System.out.println("---"+name + "到达终点..");
    }
}
// 乌龟线程
class Turtle extends Thread {
    private int distance;
    public Turtle(String name) {
        super(name);
    }
    @Override
    public void run() {
        String name = Thread.currentThread().getName();
        // 取一个随机数
        while (distance<100) {
            double d = Math.random();
            if(d>=0.3){
                distance++;
                System.out.println(name+"跑了"+distance+"米");
            }
        }
        System.out.println(name+"到达终点..");
    }
}

